package com.trust.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trust.agri.util.BeanUtil;
import com.trust.entity.Dictionary;
import com.trust.entity.SetupLand;
import com.trust.entity.VO.DictionaryVO;
import com.trust.entity.VO.LandDropDownVO;
import com.trust.entity.VO.LandSelectVO;
import com.trust.mapper.SetupLandMapper;
import com.trust.service.DictionaryService;
import com.trust.service.SetupLandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SetupLandServiceImpl extends ServiceImpl<SetupLandMapper, SetupLand> implements SetupLandService {

    @Resource
    private DictionaryService dictionaryService;

    @Override
    public String getCodeById(Integer id) {
        SetupLand land = this.getById(id);
        if (null == land){
            return "";
        }
        return land.getCode();
    }


    @Override
    public List<SetupLand> listAll() {
        return this.list();
    }

    @Override
    public LandSelectVO DropDownList() {
        LandSelectVO vo = new LandSelectVO();

        //颜色
        List<DictionaryVO> colors = dictionaryService.dropDownList(97);
        Set<String> colorStr = colors.stream().map(DictionaryVO::getName).collect(Collectors.toSet());
        vo.setColorList(colorStr);

        //未选择地块
        vo.setWhiteColor(dictionaryService.getNameById(110));

        //地块分组
        List<DictionaryVO> landGroups = dictionaryService.dropDownList(59);
        vo.setLandGroups(landGroups);

        //地块列表
        List<SetupLand> list = this.list();
        List<LandDropDownVO> landList = new ArrayList<>();
        Set<String> plantColors = new HashSet<>();
        Set<String> breedColors = new HashSet<>();
        for (SetupLand land : list) {
            LandDropDownVO target = BeanUtil.source2target(land, new LandDropDownVO());
            if (null != land.getPlantCateId() && null != land.getPlantType()){
                target.setEnabled(0);
                if (land.getPlantType().equals(1)){
                    //种植地块
                    plantColors.add(land.getColor());
                } else if (land.getPlantType().equals(2)){
                    //育苗地块
                    breedColors.add(land.getColor());
                }
            } else {
                target.setEnabled(1);
            }
            List<DictionaryVO> groupIds = JSON.parseArray(land.getBaseGroup(), DictionaryVO.class);
            List<DictionaryVO> baseGroups = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(groupIds)){
                for (DictionaryVO integer : groupIds) {
                    Dictionary dictionary = dictionaryService.getById(integer.getId());
                    DictionaryVO dictionaryVO = BeanUtil.source2target(dictionary, new DictionaryVO());
                    baseGroups.add(dictionaryVO);
                }
            }
            target.setTagList(baseGroups);
            target.setName(target.getCode());

            landList.add(target);
        }
        vo.setLandList(landList);
        vo.setPlantColors(plantColors);
        vo.setBreedColors(breedColors);
        return vo;
    }
}
